package com.fan.shop.views.bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.fan.shop.domain.User;
import com.fan.shop.services.UserService;


@ManagedBean(name = "users")
@SessionScoped
public class UsersBean extends ShopBean {

	private static Logger logger = Logger.getLogger("UsersBean");
	 
	@ManagedProperty(value = "#{userService}")
	private UserService userService;
	private List<User> users;
	private String editUser;


	public UserService getUserService() {
		logger.debug("getUserService()");
		return userService;
	}

	public void setUserService(UserService userService) {
		logger.debug("setUserService()");
		this.userService = userService;
	}

	public String getEditUser() {
		logger.debug("getEditUser()");
		return editUser;
	}

	public void setEditUser(String editUser) {
		logger.debug("setEditUser()");
		this.editUser = editUser;
	}

	public List<User> getUsers() {
		logger.debug("getUsers()");
		users = userService.getAllUsers();
		logger.debug(users.size() + "");
		return users;
	}

	public void setUsers(List<User> users) {
		logger.debug("setUsers()");
		this.users = users;
	}
	
	public String editUserAction(String username)
	{
		logger.debug("editUserAction()");
		logger.debug("User: " + username + " is going to be edited");
		FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("editUser", username);
		return "management/user";
	}
	
	public String deleteUserAction(String username)
	{
		logger.debug("deleteUserAction()");
		logger.debug("User: " + username + " is going to be deleted");
		User user = userService.getUser(username);
		
		userService.deleteUser(user);
		return "management/users";
	}
	
	
	
}
